<?php
namespace app\admin\controller\goods;

use think\Db;
class Goods extends \app\admin\controller\Common
{
    public function index(){
    
        return view();
    }

    public function list(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
       
        $query = (new \app\common\model\Goods());

        isset($search['agent_name']) && $search['agent_name'] != "" ? $query->where("agent.name like '%{$search['agent_name']}%'") : '';
        isset($search['order_status']) && $search['order_status'] != "" ? $query->where("order.order_status like '%{$search['order_status']}%'") : '';
        !empty($search['bus_id']) && $search['bus_id'] != "" ? $query->where("bus_id" ,$search['bus_id']) : '';
        $query->order("id desc");
        $where = [];
        if(!empty($search['bus_id'])){
            $where['bus_id'] = $search['bus_id'];
        }
        $list = $query->limit($page, $limit)->where($where)->append(['cate_title','brand_title','car_title'])->select();
        $count = $query->count();
        return self::successResult('',$list,$count);
    }

    public function add(){
     
        if (request()->isPost()) {
            $info = request()->param('info');
            $sku_data = request()->param('sku_data');
            Db::startTrans();
            try{
                $message = "订单创建失败,请重试";
                $process = true;
                $info['image'] = json_encode($info['image']);
                $row =  Db::table("{$this->tdb}goods")->insertGetId($info);
                if(!$row){
                    $message = "添加商品失败";
                    $process = false;
                }
                foreach ($sku_data as $key => &$value) {
                        $value['goods_id'] = $row;
                }
          
                $row =  Db::table("{$this->tdb}goods_sku")->insertAll($sku_data);
                if(!$row){
                    $message = "添加sku失败";
                    $process = false;
                }
               
                if ($process !== false) {
                    Db::commit(); // 提交事务
                } else {
                    Db::rollback(); // 回滚事务
                }

            } catch (\Exception $e) {
                $process = false;
                $message = $e->getMessage();
            
                Db::rollback(); // 回滚事务
            }
     
            return self::successResult();
        } else {
            $cate = (new \app\common\model\Classify())->lists();
            $this->assign("cate",$cate);
            $level = (new \app\common\model\Level())->select();
            $this->assign("level",$level);
            return view();
        }

    }

    
    public function edit(){
     
        if (request()->isPost()) {
            $info = request()->param('info');
            $id = request()->param('id');
            $sku_data = request()->param('sku_data');
            Db::startTrans();
            try{
                $message = "订单创建失败,请重试";
                $process = true;
                $info['image'] = json_encode($info['image']);
                $row =  Db::table("{$this->tdb}goods")->where(['id' => $id])->update($info);
                if(!$row){
                    $message = "添加商品失败";
                    $process = false;
                }
                $row =  Db::table("{$this->tdb}agent_goods")->where(['goods_id' => $id])->update($info);
                if(!$row){
                    $message = "添加商品失败";
                    $process = false;
                }

                $row =  Db::table("{$this->tdb}bus_goods")->where(['goods_id' => $id])->update($info);
                if(!$row){
                    $message = "添加商品失败";
                    $process = false;
                }
                // foreach ($sku_data as $key => &$value) {
                //         $value['goods_id'] = $row;
                // }
          
                // $row =  Db::table("{$this->tdb}goods_sku")->insertAll($sku_data);
                // if(!$row){
                //     $message = "添加sku失败";
                //     $process = false;
                // }
               
                if ($process !== false) {
                    Db::commit(); // 提交事务
                } else {
                    Db::rollback(); // 回滚事务
                }

            } catch (\Exception $e) {
                $process = false;
                $message = $e->getMessage();
                Db::rollback(); // 回滚事务
            }
        
            return self::successResult();
        } else {
            $id = request()->param('id');
            $cate = (new \app\common\model\Classify())->lists();
            $this->assign("cate",$cate);

            $goods = (new \app\common\model\Goods())->append(['brand_title','car_title','goods_type_title'])->where(['id' => $id])->find();
            $this->assign("goods",$goods);
          
            $sku = (new \app\common\model\GoodsSku())->where(['goods_id' => $id])->select();
            $this->assign("sku",$sku);
            $level = (new \app\common\model\Level())->select();
            $this->assign("level",$level);
            return view();
        }

    }
   public function get_level(){
         
        return self::successResult("",[
            0 => [
                'id' => 1, 'level' => 1 ,'name' => '1111',
            ]
        ]);
   }
   public function delete(){
        $id = request()->param('id');
        if (empty($id)) $this->error("请选择要删除的数据");
        $table = new \app\common\model\Goods();
        if (is_array($id)) {
            $id = implode(",", $id);
            $row = $table->whereIn('id', $id)->delete();
        } else {
            $row = $table->where('id', $id)->delete();
        }
        return $row ? self::successResult("成功") : self::errorResult("失败");
   }
  
   public function is_index()
   {
       $id = request()->param('id');
       $is_index = request()->param('is_index');
       if (empty($id)) $this->error("请选择要修改的数据");
       $table = new \app\common\model\Goods();
       $row = $table->where('id', $id)->update(['is_index' => $is_index]);
       return $row ? self::successResult() : self::errorResult('操作失败!请刷新后重试！');
   }
 
}
